﻿using System;
using Controller;
using WeifenLuo.WinFormsUI.Docking;
using System.Windows.Forms;
namespace CodeFactory
{
    public partial class ServerExplorer : ToolWindow
    {

        #region 私有字段

        private MainStudio mainStudio;
        
        #endregion


        #region 属性
        private ServerExplorerBO _serverBO;
        public ServerExplorerBO ServerBO
        {
            get 
            {
                if (_serverBO == null)
                    _serverBO = FactoryControllerBO.CurrentFA.CreateServerExplorerBOInstance();
                return _serverBO; 
            }
        }
        #endregion


        #region 初始化

        public ServerExplorer()
        {
            InitializeComponent();
        }
        public ServerExplorer(MainStudio _mainStudio)
        {
            InitializeComponent();
            mainStudio = _mainStudio;
        }

        private void ServerExplorer_Load(object sender, EventArgs e)
        {
            TreeMenuDataBind();
        }


        #endregion


        #region 私有方法
       
        private void TreeMenuDataBind()
        {
            treeServer.Nodes.Clear();
            ServerBO.LoadMenuConfigToTree(treeServer, imglTree);
        }

        #endregion


        #region 按钮事件

        private void tsBtnAddConnect_Click(object sender, EventArgs e)
        {
            using (ServerConnecter sc = new ServerConnecter(mainStudio))
            {
                sc.ShowDialog();
                TreeMenuDataBind();
            }
        }

       
        private void menuBuild_Click(object sender, EventArgs e)
        {
            if (treeServer.Nodes.Count > 0)
            {
                string configName = "";
                if (treeServer.SelectedNode.Parent == null)
                {
                    configName = treeServer.SelectedNode.Text;
                }
                else
                {
                    configName = treeServer.SelectedNode.Parent.Text;
                }
                using (ControlSelector cs = new ControlSelector(configName,mainStudio))
                {
                    cs.ShowDialog();
                }
            }
        }


        private void menuDel_Click(object sender, EventArgs e)
        {
            if (treeServer.Nodes.Count > 0)
            {
                string configName = "";
                if (treeServer.SelectedNode.Parent == null)
                {
                    configName = treeServer.SelectedNode.Text;
                }
                else
                {
                    configName = treeServer.SelectedNode.Parent.Text;
                }

                ServerBO.DeleteConfig(configName);
                TreeMenuDataBind();
            }
        }


        private void tsBtnRefresh_Click(object sender, EventArgs e)
        {
            TreeMenuDataBind();
        }

        #endregion

        
        

    }
}
